package org.dht.com.msg;

/**
 * The {@code SequenceGenerator} class is responsible for generating the
 * sequence numbers for the messages. This create a sequence number between 1
 * and Long.MAX_VALUE. When the sequence number is bigger than Long.MAX_VALUE
 * the sequence number starts at 0 again. The sequence number increments in one
 * every time that {@code SequenceGenerator.getSequenceNumber()} is called
 * 
 */
public class SequenceGenerator {
	/**
	 * The next sequence number, star at 0
	 */
	private static long number = 0;

	/**
	 * The max number that SequenceGenerator can generate, its value is
	 * Long.MAX_VALUE
	 */
	private static long maxNumber = Long.MAX_VALUE;

	/**
	 * This method return the next sequence number
	 * 
	 * @return Returns the next sequence number
	 */
	public synchronized static long getSequenceNumber() {
		if (number > maxNumber) {
			number = 0;
		}

		return ++number;
	}
}
